package io.gbznt.modules.buss.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.gbznt.common.utils.PageUtils;
import io.gbznt.common.utils.R;
import io.gbznt.common.utils.ShiroUtils;
import io.gbznt.common.validator.ValidatorUtils;
import io.gbznt.common.validator.group.AddGroup;
import io.gbznt.modules.buss.entity.BussFarmlandInfoEntity;
import io.gbznt.modules.buss.entity.BussFarmlandPutIntoEntity;
import io.gbznt.modules.buss.service.BussFarmlandInfoService;
import io.gbznt.modules.buss.service.BussFarmlandPutIntoService;



/**
 * 田块投入品记录
 *
 * @author gbznt
 * @email http://www.gbznt.com/
 * @date 2019-04-20 15:48:34
 */
@RestController
@RequestMapping("buss/bussfarmlandputinto")
public class BussFarmlandPutIntoController {
    @Autowired
    private BussFarmlandPutIntoService bussFarmlandPutIntoService;
    @Autowired
    private BussFarmlandInfoService binfo;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("buss:bussfarmlandputinto:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bussFarmlandPutIntoService.queryPage(params);

        return R.ok().put("page", page);
    }
    
    /**
     * 列表
     */
    @RequestMapping("/list2")
    //@RequiresPermissions("buss:bussfarmlandputinto:list")
    public R list2(@RequestParam Map<String, Object> params){
        PageUtils page = bussFarmlandPutIntoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("buss:bussfarmlandputinto:info")
    public R info(@PathVariable("id") String id){
        BussFarmlandPutIntoEntity bussFarmlandPutInto = bussFarmlandPutIntoService.getById(id);
        String tk = bussFarmlandPutInto.getFarmlandInfoId();
        if(StringUtils.isNotBlank(tk)){
        	BussFarmlandInfoEntity bi =  binfo.getById(tk);
        	if(bi!=null){
        		bussFarmlandPutInto.setFarmlandInfoIdname(bi.getName());
        	}
        	
        }
        
        return R.ok().put("bussFarmlandPutInto", bussFarmlandPutInto);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("buss:bussfarmlandputinto:save")
    public R save(@RequestBody BussFarmlandPutIntoEntity bussFarmlandPutInto){
    	ValidatorUtils.validateEntity(bussFarmlandPutInto,AddGroup.class);
    
   	 	bussFarmlandPutInto.setCreateId(ShiroUtils.getUserId()+"");
    	bussFarmlandPutInto.setCreateTime(new Date());
    	//添加部门id
    	String tk = bussFarmlandPutInto.getFarmlandInfoId();
    	if(StringUtils.isNotBlank(tk)){
    		BussFarmlandInfoEntity be =  binfo.getById(tk);
    		if(be!=null){
    			bussFarmlandPutInto.setDeptId(be.getDeptId());
    		}
    	}
    	
        bussFarmlandPutIntoService.save(bussFarmlandPutInto);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("buss:bussfarmlandputinto:update")
    public R update(@RequestBody BussFarmlandPutIntoEntity bussFarmlandPutInto){
        ValidatorUtils.validateEntity(bussFarmlandPutInto,AddGroup.class);
       
        bussFarmlandPutInto.setUpdateId(ShiroUtils.getUserId()+"");
    	bussFarmlandPutInto.setUpdateTime(new Date());
    	//添加部门id
    	String tk = bussFarmlandPutInto.getFarmlandInfoId();
    	if(StringUtils.isNotBlank(tk)){
    		BussFarmlandInfoEntity be =  binfo.getById(tk);
    		if(be!=null){
    			bussFarmlandPutInto.setDeptId(be.getDeptId());
    		}
    	}
    	
        Boolean re = bussFarmlandPutIntoService.updateById(bussFarmlandPutInto);//全部更新
        
        return re?R.ok():R.oldVersion();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("buss:bussfarmlandputinto:delete")
    public R delete(@RequestBody String[] ids){
        bussFarmlandPutIntoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
